const Koa = require('koa');
const route = require('koa-route');
const compose = require('koa-compose');
const app = new Koa();

const handler = async (ctx,next)=>{
    try {
        await next();
    } catch (err) {
        ctx.response.status = err.statusCode || err.status || 500;
        ctx.response.type = 'html';
        ctx.response.body = '<p>something is wrong</p>';
        ctx.app.emit('error', err, ctx);
    }
}

const e500_thrower = ctx =>{
    ctx.throw(500);
};
app.on('error',(err,ctx)=>{
    console.log('server error:'+ err.message);
});

app.use(compose([handler,e500_thrower]));

app.listen(3006);
